package com.yuda.tools.authreport.controller;

import cn.hutool.core.util.ZipUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.UploadFileRequest;
import com.yuda.common.core.utils.file.ZipUtils;
import com.yuda.conf.OssConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;
import java.util.List;

@RestController
public class PacageZipController {


    @Autowired
    private OSS ossClientBeiJingInner;

    @GetMapping("/packing/{directoryName}")
    public void pack(@PathVariable("directoryName") String directoryName, HttpServletResponse response) throws Throwable {

        String srcPath = "/mnt/auth_pdf/download/" + directoryName + "/";
        long start = System.currentTimeMillis();

        File zip = ZipUtil.zip(srcPath, "/mnt/" + directoryName + ".zip");
        long end = System.currentTimeMillis();
        System.out.println("打包耗时： " + (end - start) / 1000 + " 秒");
        start = System.currentTimeMillis();
        //上传oss
        InputStream inputStream = new FileInputStream(zip);
        ossClientBeiJingInner.putObject("auth-report-zip", directoryName + ".zip", inputStream);
        end = System.currentTimeMillis();
        System.out.println("上传耗时： " + (end - start) / 1000 + " 秒");

        start = System.currentTimeMillis();
        zip.delete();
        end = System.currentTimeMillis();
        System.out.println("删除耗时： " + (end - start) / 1000 + " 秒");

        response.sendRedirect("https://auth-report-zip.oss-cn-beijing.aliyuncs.com/" + zip.getName());
//        List fileList = Arrays.asList(tempList);
//        try {
//            ZipUtils.toZip(fileList,response.getOutputStream());
//        } catch (IOException e) {
//            e.printStackTrace();
//        }


    }


}
